package com.study.demo.controller;

import com.study.demo.dto.User;
import com.study.demo.repository.UserRepository;
import com.study.demo.service.JpaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/jpa")
public class JPAController {

    @Autowired
    private JpaService jpaService;

    @GetMapping("/find")
    public User find() {
        return jpaService.findByNameAndAge("1", 1);
    }

    @GetMapping("/insert")
    public String insert() {
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setName("2");
            user.setAge(2);
            user.setPassword("2");
            user.setSex(2);
            jpaService.insert(user);
        }
        return "success";
    }

    //分页查找
    @GetMapping("/findByAge")
    public List<User> getByAge() {
        return jpaService.findByAge(2, 3, 3);
    }

    //排序
    @GetMapping("/findFirstByOrderByAgeAsc")
    public User findFirstByOrderByAgeAsc() {
        return jpaService.findFirstByOrderByAgeAsc();
    }

    //自定义查询
    @GetMapping("/query")
    public User findByNameAndSex(@RequestParam(name = "name") String name,
                                 @RequestParam(name = "sex") Integer sex) {
        return jpaService.findByNameAndSex(name, sex);
    }
}
